
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.utils.FormatUtil;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.GrouponListItem;

import java.util.List;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class GrouponListViewBinder
        extends ItemViewBinder<GrouponListItem, GrouponListViewBinder.ViewHolder> implements View.OnClickListener {

    public interface OnBuyerListener {
        void onBuyer(View view, int position, GrouponListItem data);
    }

    OnBuyerListener onBuyerListener;
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnBuyerListener getOnBuyerListener() {
        return onBuyerListener;
    }

    public void setOnBuyerListener(OnBuyerListener onBuyerListener) {
        this.onBuyerListener = onBuyerListener;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_groupon_list, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull GrouponListItem post) {

        holder.ivSelect.setTag(R.id.item_key_status, post);

        holder.ivSelect.setTag(R.id.item_key_position, getPosition(holder));

        holder.ivSelect.setOnClickListener(this);
        holder.setData(post);
    }

    public String grouponIdSelected;
    public void updateData(String id){
        List<?> items = getAdapter().getItems();
        boolean isSelectedGroup = false;
        if (items != null){
            int size = items.size();
            for (int i=0;i<size;i++){
                Object o = items.get(i);
                if (o != null && o instanceof GrouponListItem){
                    GrouponListItem gli = (GrouponListItem) o;

                    if (id != null && id.equals(gli.getGrouponId())){
                        gli.setSelected(true);
                        isSelectedGroup = true;
                        grouponIdSelected = id;
                    }else {
                        gli.setSelected(false);
                    }
                }
            }
        }
        if (!isSelectedGroup){
            grouponIdSelected = null;
        }
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);

            switch (v.getId()) {
                case R.id.iv_select:

                    if (onBuyerListener != null && o instanceof GrouponListItem) {
                        onBuyerListener.onBuyer(v, position, (GrouponListItem) o);
                    }
                    break;

                default:
                    if (onRecyclerViewItemClickListener != null && o instanceof GrouponListItem) {
                        onRecyclerViewItemClickListener.onItemClick(v, position, o);
                    }
                    break;
            }


        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvAddress1;
        TextView tvAddress2;
        TextView tvDeliveryDate;
        TextView tvLimitAmount;
        TextView tvAmount;
        TextView tvAgent;
        TextView tvMatch;
        ImageView ivSelect;
        ViewHolder(@NonNull View itemView) {
            super(itemView);


            tvAddress1 = itemView.findViewById(R.id.tv_address_1);
            tvAddress2 = itemView.findViewById(R.id.tv_address_2);
            tvDeliveryDate = itemView.findViewById(R.id.tv_delivery_date);
            tvLimitAmount = itemView.findViewById(R.id.tv_limit_amount);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvAgent = itemView.findViewById(R.id.tv_agent);
            tvMatch = itemView.findViewById(R.id.tv_match);
            ivSelect = itemView.findViewById(R.id.iv_select);
        }


        void setData(GrouponListItem post) {


            tvAddress1.setText(post.getBaseCityName());
            tvAddress2.setText(post.getAgencyCityName());
            tvDeliveryDate.setText("预计 "+post.getDeliverTime()+" 发货");
            tvAgent.setText("代办：" + post.getAgencyConsigneeAddress());

            String chargeUnit = post.getChargeUnit();
            String s = "满" + FormatUtil.subZeroAndDot(post.getCountLower()) + chargeUnit + "发货，可拼" + FormatUtil.subZeroAndDot(post.getCountUpper()) + chargeUnit;
            tvLimitAmount.setText(s);
            s = "已拼" + FormatUtil.subZeroAndDot(post.getSumProductCount()) + chargeUnit + "（还剩："  + FormatUtil.subZeroAndDot(post.getRemainCount()) + chargeUnit+ "）";
            tvAmount.setText(s);

            if (!post.isSelected()) {
                ivSelect.setImageResource(R.mipmap.register_unread);
            } else {
                ivSelect.setImageResource(R.mipmap.register_read);

            }

            tvMatch.setText(post.getMatchWord());
//            tvLimitAmount.setText();

        }
    }

}
